package com.usian.controller;

import com.usian.appointments.Appointments;
import com.usian.service.TravelScheduleService;
import com.usian.travelSchedule.ScenicSpotInfo;
import com.usian.travelSchedule.TravelSchedule;
import com.usian.travelSchedule.Vo.TravelScheduleVO;
import com.usian.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateUtil.formatDateTime;

/**
 * @author 米西米西
 */
@RestController
@RequestMapping("/app/travelSchedule")
public class TravelScheduleController {

    @Autowired
    private TravelScheduleService travelScheduleService;

    /**
     * 接口：用用户id查询行程
     */
    @GetMapping("/selectTravel/{id}")
    public Result selectTravel(@PathVariable("id") Integer id){
        TravelScheduleVO travelSchedules = travelScheduleService.selectTravel(id);
        return Result.success(travelSchedules);
    }


    /**
     * 接口：用用户id查询行程
     */
    @GetMapping("/selectTravels/{id}")
    public Result selectTravels(@PathVariable("id") Integer id){
        List<TravelSchedule> travelSchedules = travelScheduleService.selectTravels(id);
        return Result.success(travelSchedules);
    }

    /**
     * 接口：根据行程id查询景点详情
     */
    @GetMapping("/selectScenicSpotInfo/{scenicSpotId}")
    public Result selectScenicSpotInfo(@PathVariable("scenicSpotId") Integer id){
        ScenicSpotInfo travelSchedules = travelScheduleService.selectScenicSpotInfo(id);
        return Result.success(travelSchedules);
    }

/**
 * 保存旅行计划
 *
 * @param travelSchedule 旅行计划对象，包含旅行的开始时间、结束时间和出发时间
 * @return 保存结果，包括是否成功保存旅行计划
 * @throws ParseException 如果日期格式转换失败
 *
 * 此方法负责接收通过HTTP POST请求发送的旅行计划数据，
 * 并将其保存到系统中。它首先格式化旅行计划的日期和时间，
 * 然后调用服务层方法进行实际的保存操作。
 */
@PostMapping("/save")
public Result save(@RequestBody TravelSchedule travelSchedule) throws ParseException {
    // 创建日期格式化对象，用于日期字符串和Date对象之间的转换
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 格式化并设置旅行开始时间
    String startDateStr = formatDateTime(travelSchedule.getStartDate());
    if (startDateStr != null) {
        travelSchedule.setStartDate(sdf.parse(startDateStr));
    }

    // 格式化并设置旅行结束时间
    String endDateStr = formatDateTime(travelSchedule.getEndDate());
    if (endDateStr != null) {
        travelSchedule.setEndDate(sdf.parse(endDateStr));
    }

    // 格式化并设置旅行出发时间
    String departureTimeStr = formatDateTime(travelSchedule.getStartDate());
    if (departureTimeStr != null) {
        travelSchedule.setDepartureTime(sdf.parse(departureTimeStr));
    }

    // 调用服务层方法保存旅行计划，并返回保存结果
    return Result.success(travelScheduleService.save(travelSchedule));
}
    /**
     * 接口：根据用户id来查询相伴出行数据
     */
    @GetMapping("/selectAppointments/{userId}")
    public Result selectAppointments(@PathVariable("userId") Integer id){
        List<Appointments> appointmentsList = travelScheduleService.selectAppointments(id);
        return Result.success(appointmentsList);
    }


    /**
     * 接口：根据出行id来删除相伴出行数据
     */
    @GetMapping("/deleteAppointment/{id}")
    public Result deleteAppointment(@PathVariable("id") Integer id){
        int i = travelScheduleService.deleteAppointment(id);
        if (i > 0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/deleteTravelSchedule/{id}")
    public Result deleteTravelSchedule(@PathVariable("id") Integer id){
        int i = travelScheduleService.deleteTravelSchedule(id);
        if (i > 0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Appointments appointments) {
        travelScheduleService.add(appointments);
        return Result.success();
    }
}
